﻿using System;
using System.Linq;
using System.Web.Mvc;
using ASPItalia.ModelVirtualCasting.Common;
using ASPItalia.ModelVirtualCasting.Web.Models;
using ASPItalia.ModelVirtualCasting.ObjectModel;
using System.Collections.Generic;

namespace ASPItalia.ModelVirtualCasting.Web.Filters
{
    public class ProvideCountriesAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);

            List<Country> res = filterContext.HttpContext.Cache["countries.all"]
                as List<Country>;
            if (res == null)
            {
                ICountryRepository countryRepository = ApplicationContext.Current.Countries;
                filterContext.HttpContext.Cache.Insert("countries.all",
                    res = countryRepository.GetAll().ToList(),
                    null,
                    DateTime.MaxValue, TimeSpan.FromMinutes(30));
            }

            filterContext.Controller.ViewData["Countries"] = res;
        }
    }
}